<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>登录页面</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <link rel="stylesheet" href="css/main.css" media="all">
    <link rel="stylesheet" href="css/plugins/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
</head>

<body class="layui-layout-login">
<div class="login-bg">
    <div class="cover"></div>
</div>
<div class="login-content" >
    <h1 class="login-box-title"><i class="fa fa-fw fa-user"></i>登录</h1>
    <form class="layui-form" id="loginForm" lay-filter="loginForm">

        <div class="layui-form-item">
            <label class="layui-icon layui-icon-username" for="username"></label>
            <input class="layui-input" type="text" name="username" id="username" placeholder="用户名" lay-verify="required">
        </div>

        <div class="layui-form-item">
            <label class="layui-icon layui-icon-password" for="password"></label>
            <input class="layui-input" type="password" name="password" id="password" placeholder="密码" lay-verify="required">
        </div>

        <div class="layui-form-item">
            <label class="layui-icon layui-icon-cellphone" for="mobile"></label>
            <input class="layui-input" type="text" name="mobile" id="mobile" placeholder="手机号" lay-verify="required|phone|number">
        </div>

        <div class="layui-form-item captcha-item">
            <label class="layui-icon layui-icon-vercode"></label>
            <input class="layui-input"  type="text" id="code" name="code" autocomplete="off" placeholder="验证码" lay-verify="required">
            <button type="button" style="margin-top: 3px;margin-left: 10px" class="layui-btn layui-btn-sm" id="msg">获取短信验证码</button>
        </div>

        <button type="button" lay-filter="loginSubmit" lay-submit  id="login" class="layui-btn layui-btn-fluid ajax-login"><i class="fa fa-sign-in fa-lg fa-fw"></i> 登录</button>
    </form>
</div>
</body>
<script src="layui/layui.js" type="text/javascript"></script>
<script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.cookie.js"></script>
<script type="text/javascript">
    /**
     * 动态获取项目路径
     * */
    function getPath(){
        var pathname = document.location.pathname;
        var index = pathname.lastIndexOf("/");
        var path = pathname.substring(0,index);
        $("#path").val(path);
    }


    layui.use(['layer','form'],function () {
        var layer = layui.layer;
        var form = layui.form;
        /**
         * 监听登陆按钮
         * */
        form.on("submit(loginSubmit)",function (obj) {
                $.ajax({
                    url:'house-user/loginUser',
                    data:{
                        username:$("#username").val(),
                        password:$("#password").val(),
                        code:$("#code").val(),
                        mobile: $("#mobile").val()
                    },
                    type:"post",
                    dataType:"json",
                    success:function (data) {
                        if(data.code==200){
                            layer.msg("登陆成功，即将进入...",{icon:'1',title:'提示',closeBtn :'1'});
                            location.href = 'house-main.html';
                        }else{
                            layer.msg(data.message,{icon:'2',title:'提示',closeBtn :'1'});
                        }
                    }
                });
            return false;
        });



        var validCode=true;
        $("#msg").click(function () {
            $.ajax({
                url:'house-user/send',
                data: {
                    mobile:$("#mobile").val()
                },
                type:"get",
                dataType: "json",
                success:function (resp) {
                    if (resp.code==200) {
                        var time = resp.data;
                        var code=$("#msg");
                        if (validCode) {
                            validCode=false;
                            code.prop("disabled", true);
                            var t=setInterval(function  () {
                                time--;
                                code.html(time+"秒");
                                if (time==0) {
                                    clearInterval(t);
                                    code.html("重新获取");
                                    validCode=true;
                                    code.prop("disabled", false);
                                }
                            },1000);
                        }
                    }else {
                        layer.msg(resp.message,{icon:'2',title:'提示',closeBtn :'1'});
                    }
                }
            });

        });




    });


</script>
</html>
